package creational.builder;

import java.math.BigDecimal;

/**
 * 建造者模式
 */


class Product{
    private String name;
    private BigDecimal price;
    //......

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

abstract class Builder {
    protected Product product = new Product();

    public abstract void setName(String name);

    public abstract void setPrice(BigDecimal price);

    public Product getProduct() {
        return product;
    }
}

class Director{
    private Builder builder;

    public Director(Builder builder){
        this.builder = builder;
    }

    public Product initProduct(String name,BigDecimal price){
        /**
         * 公共处理
         */
        builder.setName(name);
        builder.setPrice(price);
        return builder.getProduct();
    }
}

class ChineseProductBuilder extends Builder {

    @Override
    public void setName(String name) {
        /**
         * 处理中文名称
         */
        name = name + "-->中文";
        this.product.setName(name);
    }

    @Override
    public void setPrice(BigDecimal price) {
        /**
         * 处理人民币
         */
        //.....
        this.product.setPrice(price);
    }
}

class AmericaProductBuilder extends Builder{
    @Override
    public void setName(String name) {
        /**
         * 处理英文名称
         */
        name = name + "-->English";
        this.product.setName(name);
    }

    @Override
    public void setPrice(BigDecimal price) {
        /**
         * 处理美元
         */
        //.....
        this.product.setPrice(price);
    }
}


class Test{
    public static void main(String[] args) {
        ChineseProductBuilder chineseProductBuilder = new ChineseProductBuilder();
        AmericaProductBuilder americaProductBuilder = new AmericaProductBuilder();

        Director director = new Director(chineseProductBuilder);
        Product product = director.initProduct("哇哈哈", BigDecimal.valueOf(10));
        System.out.println(product);

        Director director1 = new Director(americaProductBuilder);
        Product product1 = director1.initProduct("wahaha", BigDecimal.valueOf(2));
        System.out.println(product1);

    }
}
